<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/3/16
 * Time: 17:51
 */

namespace app\api\controller;


use think\Db;

class Registershop extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 判断用户店铺注册状态
     * @return \think\response\Json
     */
    public function registerStatus(){
        $exist = Db::table('up_bo_shop_check')->where('bsc_user_id', $this->user_id)->field('bsc_status, bsc_refuse_reason')->find();
        if(!$exist){
            $res = ['bsc_status' => 0, 'refuse_reason' => '']; // 未申请
        }else{
            // 1 待审核   2 审核通过   3 审核失败
            $res = ['bsc_status' => $exist['bsc_status'], 'refuse_reason' => $exist['bsc_refuse_reason']];
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    
    /**
     * 申请信息回显
     * @return \think\response\Json
     */
    public function showApply(){
        $apply = Db::table('up_bo_shop_check')
            ->join('up_area u1', 'u1.district_id = bsc_sheng', 'LEFT')
            ->join('up_area u2', 'u2.district_id = bsc_shi', 'LEFT')
            ->join('up_area u3', 'u3.district_id = bsc_qu', 'LEFT')
            ->where('bsc_user_id', $this->user_id)
            ->field('bsc_role, bsc_id, bsc_shop_name, bsc_sheng, bsc_shi, bsc_qu, bsc_address, bsc_lon, 
            bsc_lat, bsc_user_name, bsc_user_phone, u1.district sheng, u2.district shi, u3.district qu')
            ->find();
        return json(['status' => 200, 'msg' => $apply]);
    }
    
    /**
     * 提交注册店铺申请
     * @return \think\response\Json
     * bsc_role          角色  3 区域加盟商  4 县级运营商  5 市级分公司
     * bsc_shop_name     店铺名称
     * bsc_sheng         省ID
     * bsc_shi           市ID
     * bsc_qu            区ID
     * bsc_address       详细地址
     * bsc_lon           经度
     * bsc_lat           纬度
     * bsc_user_name     联系人
     * bsc_user_phone    联系手机号
     */
    public function applyShop(){
        $bsc_role = input('bsc_role'); // 3 区域加盟商  4 县级运营商  5 市级分公司
        $bsc_shop_name = input('bsc_shop_name'); // 店铺名称
        $bsc_sheng = input('bsc_sheng'); // 省ID
        $bsc_shi = input('bsc_shi'); // 市ID
        $bsc_qu = input('bsc_qu'); // 区ID
        $bsc_address = input('bsc_address'); // 详细地址
        $bsc_lon = input('bsc_lon'); // 经度
        $bsc_lat = input('bsc_lat'); // 纬度
        $bsc_user_name = input('bsc_user_name'); // 联系人
        $bsc_user_phone = input('bsc_user_phone'); // 联系手机号
    
        if($bsc_role == 6){
            $is_apply = Db::table('up_bo_shop_check')
                ->where(['bsc_status' => 2, 'bsc_sheng' => $bsc_sheng, 'bsc_role' => 6])
                ->find();
        }elseif($bsc_role == 5){
            $is_apply = Db::table('up_bo_shop_check')
                ->where(['bsc_status' => 2, 'bsc_shi' => $bsc_shi, 'bsc_role' => 5])
                ->find();
        }else{
            $mile = Db::table('up_about')->where('id', 11)->value('content');
            $miles = (int)$mile;
            $is_apply = Db::table('up_bo_shop_check')
                ->field('ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$bsc_lat.'* PI() / 180 - bsc_lat * PI() / 180) / 2),2) + COS('.$bsc_lat.' * PI() / 180) * COS(bsc_lat * PI() / 180) * POW(SIN(('.$bsc_lon.' * PI() / 180 - bsc_lon * PI() / 180) / 2),2))) * 1000) AS distance')
                ->where(['bsc_status' => 2, 'bsc_role' => ['between', [3, 4]]])
                ->having("distance <= $miles")
                ->find();
        }
        if($is_apply){
            return json(['status' => 0, 'msg' => '区域范围内已有店铺']);
        }else{
            $exist = Db::table('up_bo_shop_check')->where('bsc_user_id', $this->user_id)->field('bsc_id, bsc_status')->find();
            $data = [
                'bsc_status' => 1,
                'bsc_role' => $bsc_role,
                'bsc_shop_name' => $bsc_shop_name,
                'bsc_sheng' => $bsc_sheng,
                'bsc_shi' => $bsc_shi,
                'bsc_qu' => $bsc_qu,
                'bsc_address' => $bsc_address,
                'bsc_lon' => $bsc_lon,
                'bsc_lat' => $bsc_lat,
                'bsc_user_name' => $bsc_user_name,
                'bsc_user_phone' => $bsc_user_phone,
                'bsc_time' => $this->create_time
            ];
            $query = Db::table('up_bo_shop_check');
            if($exist){
                if($exist['bsc_status'] != 3) return json(['status' => 0, 'msg' => '申请状态异常']);
                
                $data['bsc_id'] = $exist['bsc_id'];
                $res = $query->update($data);
            }else{
                $data['bsc_user_id'] = $this->user_id;
                $res = $query->insert($data);
            }
            if($res){
                return json(['status' => 200, 'msg' => '提交成功']);
            }else{
                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            }
        }
    }
}